package org.boge.sharpdoc.controller;

import org.boge.sharpdoc.bean.Customer;
import org.boge.sharpdoc.bean.LayuiReturnData;
import org.boge.sharpdoc.service.CareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "care")
public class CareController {
    @Autowired
    private CareService careService;
    @Autowired
    private HttpSession session;

    @GetMapping("showCare")
    public String showCare(@RequestParam(defaultValue = "1") Integer index, @RequestParam(defaultValue = "5") Integer pageSize, Model model) {
        Customer customer = (Customer) session.getAttribute("current_customer");
        model.addAttribute("cared", careService.getOnesCare(index, pageSize, customer.getPhone()));
        return "customer/myCare";
    }

    @PostMapping("removeCare")
    @ResponseBody
    public LayuiReturnData removeCare(Long other) {
        Customer s_customer = (Customer) session.getAttribute("current_customer");
        careService.removeCare(s_customer.getPhone(), other / 6);
        return new LayuiReturnData(0);
    }

    @PostMapping("addCare")
    @ResponseBody
    public LayuiReturnData addCare(Long other) {
        Customer s_customer = (Customer) session.getAttribute("current_customer");
        careService.addCare(s_customer.getPhone(), other / 6);
        return new LayuiReturnData(0);
    }
}